var sliceUploadFile = function() {
    describe('sliceUploadFile 分块上传全流程（支持暂停续传）', function () {
        describe('检查文件是否上传完整', function () {
            let key = 'test.txt';
            let file = blob({ size: 1024 * 1024 * 5.1 });
            it('上传文件', function (done) {
                this.timeout(40000);
                var lastPercent = 0;
                ks3.sliceUploadFile({
                    key,
                    body: file,
                    onProgress: function (info) {
                        lastPercent = info.percent;
                    },
                }, (err, res) => {
                    should.not.exist(err);
                    lastPercent.should.be.equal(1);
                    res.statusCode.should.be.equal(200);
                    done();
                })
            })
            it('线上文件与本地文件大小一致', function (done) {
                this.timeout(2000);
                ks3.headObject({
                    key: key,        // 填写Object完整路径，必填
                }).then(res => {
                    console.log('headObject res:', res);
                    done();
                })
            })
            // it('删除该文件', function (done) {
            //     this.timeout(2000);
            //     ks3.delObject({
            //         key
            //     }).then(res => {
            //         should.not.exist(res.error);
            //         res.statusCode.should.be.equal(204);
            //         done();
            //     })
            // })
        })
        describe('分片上传文件过程被暂停', function () {
            var key = '10m.zip';
            var alive = false;
            var paused = false;
            var taskId;
            it('上传文件正常暂停', function (done) {
                this.timeout(40000);
                ks3.sliceUploadFile({
                    key: key,
                    body: blob({ size: 1024 * 1024 * 5.1 }),
                    onTaskReady: function (_taskId) {
                        taskId = _taskId;
                    },
                    onProgress: function (info) {
                        alive = true;
                        if (!paused) {
                            if (info.percent > 0.5) {
                                ks3.pauseTask(taskId);
                                paused = true;
                                alive = false;
                                setTimeout(() => {
                                    alive.should.be.equal(false);
                                    done();
                                }, 2000)
                            }
                        }
                    }
                }, (err, res) => {
                    alive = true;
                })
            })
        })
        describe('分片上传文件过程被取消', function () {
            var key = '11m.zip';
            var alive = false;
            var canceled = false;
            var taskId;
            it('上传文件正常暂停', function (done) {
                this.timeout(40000);
                ks3.sliceUploadFile({
                    key: key,
                    body: blob({ size: 1024 * 1024 * 5.1 }),
                    onTaskReady: function (_taskId) {
                        taskId = _taskId;
                    },
                    onProgress: function (info) {
                        alive = true;
                        console.log('上传进度', info)
                        if (!canceled) {
                            if (info.percent > 0.5) {
                                ks3.cancelTask(taskId);
                                canceled = true;
                                alive = false;
                                setTimeout(() => {
                                    alive.should.be.equal(false);
                                    done();
                                }, 2000)
                            }
                        }
                    }
                }, (err, res) => {
                    alive = true;
                })
            })
        })
        describe('分块上传文件,不设置acl', function () {
            let key = 'demo.txt';
            let file = blob({ size: 1024 * 1024 * 5.1 });
            it('上传文件', function (done) {
                this.timeout(40000);
                var lastPercent = 0;
                ks3.sliceUploadFile({
                    key,
                    body: file,
                    onProgress: function (info) {
                        lastPercent = info.percent;
                        console.log('上传进度：' + info.percent)
                    },
                }, (err, res) => {
                    should.not.exist(err);
                    lastPercent.should.be.equal(1);
                    res.statusCode.should.be.equal(200);
                    done();
                })
            })
            it('获取该文件的ACL', function (done) {
                this.timeout(2000);
                ks3.getObjectAcl({
                    key
                }).then(res => {
                    let grant = res.body.AccessControlPolicy.AccessControlList.Grant;
                    grant.Permission.should.be.equal('FULL_CONTROL');
                    should.not.exist(res.error);
                    res.statusCode.should.be.equal(200);
                    done();
                })
            })
            it('删除该文件', function (done) {
                this.timeout(2000);
                ks3.delObject({
                    key
                }).then(res => {
                    should.not.exist(res.error);
                    res.statusCode.should.be.equal(204);
                    done();
                })
            })
        })
    })
}